package mysrpringboot.springboot.rocketmq;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.stereotype.Component;


@Component
public class Myproduce {

       public void produce(String id) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
           DefaultMQProducer produce = new DefaultMQProducer("helloWord_pro_01");
           produce.setNamesrvAddr("192.168.159.162:9876");
           produce.start();
           Message msg = new Message("tp_Topic_001",id.getBytes());

           try{
               //2 发送消息
               SendResult result = produce.send(msg);
               //3 打印发送结果
               System.out.println(result+"hahahah");
           }catch (Exception e){
               e.printStackTrace();
           }
           produce.shutdown();
       }
}
